import {createApp } from 'vue'
import {createI18n} from 'vue-i18n'
import App from './App.vue'
import router from './router'
import store from './store'

import 'normalize.css/normalize.css'
import 'vant/lib/icon/local.css'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
import Provider from '@/providers/provider'
import Directives from '@/utils/directives'
import VueClipboard from 'vue-clipboard3'
import VantComps from '@/utils/vantComps'
import { langs, defaultLang } from '@/lang'
import {toggleDebug} from '@/utils'

const Vue = createApp(App)
if(process.env.NODE_ENV == 'production'){
  Vue.config.errorHandler = (err, vm, info) => {
      console.error(`${info}: ${err}`)
  }
}


//全局指令引入
Directives(Vue)
// 全局注册Vant引用组件
VantComps(Vue)
// 全局注册Provider高阶插件
Vue.use(Provider)
// 全局注册VueClipboard复制插件
Vue.use(VueClipboard)


//初始国际化设置
let i18n = createI18n({
  locale: store.state.user.lang,
  fallbackLocale: defaultLang,
  messages: (()=>{
        return langs.reduce((obj, item)=>{
            obj[item.key] = item.messages
            return obj
        }, {})
    })()
})

// 全局注册以大写F开头的公共组件,
const commonComponent = require.context('./components', true, /F[A-Z]\w+\.(vue|js)$/)
commonComponent.keys().forEach(fileName => {
  const componentConfig = commonComponent(fileName)
  const componentName = upperFirst(camelCase(componentConfig.default && componentConfig.default.name || fileName .split('/') .pop() .replace(/\.\w+$/, '')))
  Vue.component(componentName, componentConfig.default || componentConfig)
})

// 非生产环境开启调试器
if(window["cordova"] && process.env.VUE_APP_CURRENTMODE!=='prod'){
  toggleDebug()
}

Vue.config.productionTip = false
Vue.use(i18n).use(store).use(router).mount('#app')

//拉取线上最新国际化
store.dispatch('user/setLang') 

const _VM = Vue.config.globalProperties //导出globalProperties 方便调用
export {_VM as VM}


